<?php

namespace App\Http\Controllers\web;

use App\Service\Authorization\LoginService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;
use lxs\api\ApiResponse;

class LoginController
{

    /**
     * 获取登录微信二维码信息
     */
    public function getWechatQrcodeData(Request $request): JsonResponse
    {

        $data['appid'] = env('PC_APP_ID');
        $data['redirectUrl'] = 'https://pcretail.ouankj.com/#/login';
        $data['state'] = rand(0, 999999);

        return ApiResponse::success('获取登录微信二维码信息成功', $data);
    }


    /**
     * 处理微信登录回调
     */
    public function getLoginWechatHandle(Request $request): JsonResponse
    {
        $code = $request->input('code') ?? '';
        $state = $request->input('state') ?? '';
        $appid = env('PC_APP_ID');
        $secret = env('PC_APP_SECRET');

        $result = LoginService::instance()->wechatQrcodeHandle($code, $state, $appid, $secret);
        if ($result['code'] == 200) {
            return ApiResponse::apiResponseData(200, '登录成功', [
                'token' => $result['data'],
            ]);
        }
        return ApiResponse::apiResponseData($result['code'], $result['message']);
    }


    /**
     * 密码登录提交
     */
    public function passwordLoginSubmit(Request $request): JsonResponse
    {
        $post = $request->input();

        $validator = Validator::make($post, [
            'mobile' => [
                'required',
                function ($attribute, $value, $fail) {
                    if (!preg_match('/^1[3-9]\d{9}$/', $value)) {
                        $fail('手机号码格式不正确');
                    }
                }
            ],
            'password' => ['required', 'min:6', 'max:20']
        ], [
            'mobile.required' => '请输入手机号码',
            'mobile.mobile' => '请输入正确的手机号码',
            'password.required' => '请输入密码',
            'password.min' => '密码长度不能小于6位',
            'password.max' => '密码长度不能大于20位',
        ]);

        if ($validator->fails()) {
            return ApiResponse::apiResponseData(5006, $validator->errors()->first());
        }

        $result = LoginService::instance()->passwordLoginSubmit($post['mobile'], $post['password'], '', 'web');

        if ($result['code'] == 200) {
            return ApiResponse::apiResponseData(200, '登录成功', [
                'token' => $result['data'],
            ]);
        }

        return ApiResponse::apiResponseData($result['code'], $result['message']);

    }


    /**
     * 验证码登录提交
     */
    public function captchaLoginSubmit(Request $request): JsonResponse
    {

        $validator = Validator::make($request->all(), [
            'mobile' => [
                'required',
                function ($attribute, $value, $fail) {
                    if (!preg_match('/^1[3-9]\d{9}$/', $value)) {
                        $fail('手机号码格式不正确');
                    }
                }
            ],
            'captcha' => [
                'required',
                function ($attribute, $value, $fail) {
                    if (Redis::get(request()->input('mobile')) != $value) {
                        $fail('验证码错误');
                    }
                }
            ],
        ], [
            'mobile.required' => '请输入手机号码',
            'mobile.mobile' => '请输入正确的手机号码',
            'captcha.required' => '请输入验证码'
        ]);

        if ($validator->fails()) {
            return ApiResponse::apiResponseData(5006, $validator->errors()->first());
        }

        $validated = $request->only(['mobile', 'code']);

        $result = LoginService::instance()->captchaLoginSubmit($validated['mobile'], $validated['code'], 'web');

        if ($result['code'] == 200) {
            return ApiResponse::apiResponseData(200, '登录成功', [
                'token' => $result['data'],
            ]);
        }

        return ApiResponse::apiResponseData($result['code'], $result['message']);

    }

}
